using QAssetBundle;
using QFramework;
using System.Linq;
using UnityEngine;

namespace ProjectSurvivor
{
    public partial class SimpleKnife : ViewController
    {

        float mCurrentSconds = 0;
        private void Update()
        {
            mCurrentSconds += Time.deltaTime;
            if (mCurrentSconds > Global.SimpleKnifeDuration.Value)
            {
                mCurrentSconds = 0;
                if (Player.Default)
                {
                    var enemies = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None).OrderBy(enemy => Player.Default.Distance2D(enemy)).Take(Global.SimpleKnifeCount.Value + Global.AdditionFlyThingCount.Value);
                    var i = 0;
                    foreach (var enemy in enemies)
                    {
                        if (i < 4)
                        {
                            ActionKit.DelayFrame(11 * i, () =>
                            {
                                AudioKit.PlaySound(Sfx.KNIFE);
                            })
                            .StartGlobal();
                            i++;
                        }
                        if (enemy)
                        {
                            knife.Instantiate().Position(this.Position()).Show().Self((self) =>
                            {
                                var selfCache = self;

                                var direction = enemy.NormalizedDirection2DFrom(Player.Default);
                                selfCache.transform.up = direction;
                                var rigidbody2D = self.GetComponent<Rigidbody2D>();
                                rigidbody2D.velocity = enemy.NormalizedDirection2DFrom(Player.Default) * 10;
                                var attackcount = 0;
                                self.OnTriggerEnter2DEvent(collider =>
                                {
                                    var hurtBox = collider.GetComponent<HurtAndHitBox>();
                                    if (hurtBox)
                                    {
                                        if (hurtBox.Owner.CompareTag("Enemy") || hurtBox.Owner.CompareTag("EnemyFly"))
                                        {
                                            var damageTimes =Global.Superknife.Value ? Random.Range(2, 3 + 1) : 1;
                                            attackcount++;
                                            DemageSystem.CalculateDemage(Global.SimpleKnifeDemage.Value * damageTimes, hurtBox.Owner.GetComponent<IEnemy>());
                                            if (attackcount >= Global.SimpleKnifeAttackCount.Value)
                                            {
                                                selfCache.DestroyGameObjGracefully();
                                            }
                                        }
                                    }
                                }).UnRegisterWhenGameObjectDestroyed(self);

                                ActionKit.OnUpdate.Register(() =>
                                {
                                    if (Player.Default)
                                    {
                                        if (Player.Default.Distance2D(selfCache) >= 20)
                                        {
                                            self.DestroyGameObjGracefully();
                                        }
                                    }
                                }).UnRegisterWhenGameObjectDestroyed(self);

                            });
                        }
                    }
                }



            }

        }
    }


}

